package com.demo.controller;

import cn.afterturn.easypoi.entity.vo.BigExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.demo.model.Order;
import com.demo.service.ExcelService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author: cyj
 **/
@RestController
public class ExcelController {

    @Resource
    private ExcelService excelService;

    /**
     * 使用easypoi实现大数据量导出excel
     * @param map
     * @param request
     * @param response
     */
    @GetMapping("/excel/download")
    public void download(ModelMap map, HttpServletRequest request,
                         HttpServletResponse response) {

        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("key", "测试参数");

        ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
        params.setFreezeCol(2);
        map.put(BigExcelConstants.CLASS, Order.class);
        map.put(BigExcelConstants.PARAMS, params);
        //就是我们的查询参数,会带到接口中,供接口查询使用
        map.put(BigExcelConstants.DATA_PARAMS, paramMap);
        map.put(BigExcelConstants.DATA_INTER,excelService);
        PoiBaseView.render(map, request, response, BigExcelConstants.EASYPOI_BIG_EXCEL_VIEW);
    }
}
